package com.yw.controller;

import com.yw.entity.User;
import com.yw.service.UserService;
import com.yw.utils.CheckResult;
import com.yw.utils.JwtUtils;
import com.yw.utils.Result;
import com.yw.utils.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created By 张永伟 on 2018/5/31.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "login")
    public Result login(String password){
        User user = userService.getUserById(3);
        if(user != null){
            if(user.getPassword().equals(password)){
                //把token返回给客户端-->客户端保存至cookie-->客户端每次请求附带cookie参数
                String JWT = JwtUtils.createJWT("1", user.getUserName(), SystemConstant.JWT_TTL);
                return Result.ok(JWT);
            }else{
                return Result.error();
            }
        }else{
            return Result.error();
        }
    }


    @RequestMapping(value = "validateJWT")
    public Result validateJWT(String jwt){
        CheckResult check = JwtUtils.validateJWT(jwt);
        if(check.isSuccess()){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

}
